package com.brkj.test;

import java.io.Serializable;
import java.util.List;

import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.FinalDb;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.brkj.model.DS_Course;
import com.brkj.model.DS_Vote;
import com.brkj.main.R;
import com.brkj.util.ConstAnts;
import com.brkj.util.ImgShow;
import com.brkj.util.TimeHelp;
import com.nostra13.universalimageloader.core.ImageLoader;

public class VoteAdapter extends BaseAdapter{

	private ListItemView listItemView;
	protected LayoutInflater listContainer; 
	private Context context;
	private List<DS_Vote> voteList;
	
	public VoteAdapter(Context context, List<DS_Vote> voteList){
		this.context = context;
		this.voteList = voteList;
		
		listContainer = LayoutInflater.from(context);
		
	}
	
	private static class ListItemView{
		public TextView Title;
		public TextView Time;
		public TextView haveDone;
		public Button doVote;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return voteList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// 自定义视图
		listItemView = new ListItemView();
		// 获取list_item布局文件的视图
		if (convertView == null) {
			convertView = listContainer.inflate(R.layout.vote_list_item, null);
			// 获取控件对象
			listItemView.Title = (TextView) convertView
					.findViewById(R.id.Title);
			listItemView.Time = (TextView) convertView
					.findViewById(R.id.Time);
			listItemView.haveDone = (TextView) convertView
					.findViewById(R.id.haveDone);
			listItemView.doVote = (Button) convertView
					.findViewById(R.id.doVote);

			// 设置控件集到convertView
			convertView.setTag(listItemView);
		} else {
			listItemView = (ListItemView) convertView.getTag();
		}
		
		
		listItemView.Title.setText(voteList.get(position).getVotePaperName());
		listItemView.Time.setText(voteList.get(position).getTime());
		
		if(voteList.get(position).getIsHaveDone() == ConstAnts.HAVE_DONE_VOTE){
			listItemView.doVote.setVisibility(View.GONE);
			listItemView.haveDone.setVisibility(View.VISIBLE);
		}else{
			listItemView.doVote.setVisibility(View.VISIBLE);
			listItemView.haveDone.setVisibility(View.GONE);
			
			listItemView.doVote.setTag(voteList.get(position));
			listItemView.doVote.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					ConfirmVoteDialog dialog = new ConfirmVoteDialog(v.getTag());
					dialog.show();
				}
			});
		}
		
		return convertView;
	}

	
	public class ConfirmVoteDialog {
		protected Button btn_ok;
		protected Button btn_cancle;
		
		protected Dialog dialog;

		public ConfirmVoteDialog(final Object course) {
			dialog = new Dialog(context, R.style.MyCustomDialog);
			dialog.setContentView(R.layout.vote_confirm_dialog);

			btn_ok = (Button) dialog.findViewById(R.id.confirm);
			btn_cancle = (Button) dialog.findViewById(R.id.cancle);
			
			btn_cancle.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					dialog.dismiss();
				}
			});
			
			btn_ok.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context, VoteDetailActivity.class);
					intent.putExtra("vote", (Serializable)course);
					context.startActivity(intent);
					dialog.dismiss();
					
				}
			});
		}
		
		
		public void show(){
			dialog.show();
		}
	}
}
